package com.ewanghuiju.app.ui.home.fragment;

import android.graphics.Rect;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.home.ChoicesContract;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.local.HomeLimitedSeckillLocalOne;
import com.ewanghuiju.app.model.bean.local.HomeLimitedSeckillLocalTop;
import com.ewanghuiju.app.model.bean.response.ArticleArticleResponBean;
import com.ewanghuiju.app.model.bean.response.LbImgBean;
import com.ewanghuiju.app.model.bean.response.MailShopResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanClassifyResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.home.ChoicesPresenter;
import com.ewanghuiju.app.test.bean.MyPageGridBean;
import com.ewanghuiju.app.ui.home.activity.InviteFriendActivity;
import com.ewanghuiju.app.ui.home.adapter.HomeLimitedSeckillAdapter;
import com.ewanghuiju.app.ui.main.activity.MainActivity;
import com.ewanghuiju.app.ui.main.activity.RedEnvelopesActivity;
import com.ewanghuiju.app.ui.message.activity.MessageListActivity;
import com.ewanghuiju.app.ui.my.activity.WalletRechargeActivity;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanHotActivity;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanHotGroupActivity;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanShopDetailsActivity;
import com.ewanghuiju.app.ui.pintuan.adapter.PintuanShopListAdapter;
import com.ewanghuiju.app.ui.shopmail.activity.TodayVogueActivity;
import com.ewanghuiju.app.ui.shopmail.adapter.MailShopAdapter;
import com.ewanghuiju.app.ui.watermail.activity.WaterHomeActivity;
import com.ewanghuiju.app.ui.watermail.activity.WaterShopDetailsActivity;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.NoDoubleClickListener;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.FixedAspectRatioLinerLayout;
import com.ewanghuiju.app.widget.GabrielleViewFlipper;
import com.ewanghuiju.app.widget.popupwindow.CustomBasePopup;
import com.ewanghuiju.app.widget.popupwindow.PopupItemClickCallback;
import com.ewanghuiju.app.widget.popupwindow.RedBagPopup;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.stx.xhb.androidx.OnDoubleClickListener;
import com.stx.xhb.androidx.XBanner;
import com.wihaohao.PageGridView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 精选页面
 * @time 2020/2/17
 */
public class ChoicestFragment extends BaseFragment<ChoicesPresenter> implements ChoicesContract.View {
    @BindView(R.id.pageView)
    PageGridView pageView;
    @BindView(R.id.xbanner)
    XBanner xbanner;
    @BindView(R.id.vf_news)
    GabrielleViewFlipper vfNews;
    @BindView(R.id.tv_go_tuanzhang)
    TextView tvGoTuanzhang;
    @BindView(R.id.tv_go_water)
    TextView tvGoWater;
    @BindView(R.id.tv_jymarket)
    TextView tvJymarket;
    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.recyclerView_tuijian)
    RecyclerView recyclerViewTuijian;
    @BindView(R.id.tv_ms_time1)
    TextView tvMsTime1;
    @BindView(R.id.tv_ms_text1)
    TextView tvMsText1;
    @BindView(R.id.tv_ms_time2)
    TextView tvMsTime2;
    @BindView(R.id.tv_ms_text2)
    TextView tvMsText2;
    @BindView(R.id.tv_ms_time3)
    TextView tvMsTime3;
    @BindView(R.id.tv_ms_text3)
    TextView tvMsText3;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView_xsms)
    RecyclerView recyclerViewXsms;
    //为您推荐图标
    @BindView(R.id.tv_wntj)
    ImageView tvWntj;
    @BindView(R.id.iv_ad)
    ImageView ivAd;
    @BindView(R.id.fix_ad)
    FixedAspectRatioLinerLayout fixAd;
    @BindView(R.id.iv_to_top)
    ImageView ivToTop;
    @BindView(R.id.lottie_redbaganim)
    LottieAnimationView lottieRedbaganim;

    private int currentPage = 1;
    private HomeLimitedSeckillLocalTop homeLimitedSeckillLocalTop;

    private PintuanShopListAdapter pintuanShopListAdapter;


    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choices;
    }

    @Override
    protected void initEventAndData() {

        mPresenter.getChoicesData(1);
        mPresenter.getChoicesData(2);
        mPresenter.getViewFlipper();
        mPresenter.getPticatesAll();
        //红包开关
        mPresenter.ptiLuckyDrawoff();

        intHotTuangouList();
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(activity);
                currentPage++;
                mPresenter.getPintuanList(currentPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                mPresenter.getChoicesData(1);
                mPresenter.getChoicesData(2);
                mPresenter.getViewFlipper();
                mPresenter.getPticatesAll();
                //红包开关
                mPresenter.ptiLuckyDrawoff();

                LoadingDialogUtils.show(activity);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getPintuanList(currentPage);
            }
        });

        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Rect scrollRect = new Rect();
                nestedScrollView.getHitRect(scrollRect);
                //子控件在可视范围内（至少有一个像素在可视范围内）
                if (scrollY <= recyclerViewTuijian.getY() && ivToTop.getVisibility() == View.VISIBLE) {//向下滚动
//                   ivToTop.animate().scaleX(1F).scaleY(1F).setDuration(500).start();
                    ivToTop.setVisibility(View.GONE);
                } else if (scrollY > recyclerViewTuijian.getY() && ivToTop.getVisibility() == View.GONE) {
                    ivToTop.setVisibility(View.VISIBLE);
                }
            }
        });

        //首次进入显示
        LoadingDialogUtils.show(activity);
        currentPage = 1;
//        refreshLayout.autoRefresh();
        mPresenter.getPintuanList(currentPage);

    }

    /**
     * 热门团购 加载商品列表
     */
    private void intHotTuangouList() {
        recyclerViewTuijian.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerViewTuijian.setLayoutManager(linearLayoutManager);
        pintuanShopListAdapter = new PintuanShopListAdapter(R.layout.item_pintuan_shop_hot);
        pintuanShopListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PintuanShopListResponBean pslBean = (PintuanShopListResponBean) adapter.getItem(position);
                new StartActivityUtil(activity, PintuanShopDetailsActivity.class).putExtra(Constants.PINTUAN_SHOP_ID,
                        pslBean.getId())
                        .startActivity(true);
            }
        });
        recyclerViewTuijian.setAdapter(pintuanShopListAdapter);
    }


    @OnClick({R.id.fiv_today_hot, R.id.fiv_gyzq, R.id.fiv_desq, R.id.layout_ms_right1, R.id.layout_ms_right2, R.id.layout_ms_right3,
            R.id.layout_hot_pintuan, R.id.layout_invite_friend, R.id.layout_new_teach, R.id.layout_go_tuanzhang,
            R.id.iv_go_tuanzhang, R.id.layout_go_water, R.id.iv_go_water, R.id.iv_to_top, R.id.lottie_redbaganim})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.fiv_today_hot:
                new StartActivityUtil(context, TodayVogueActivity.class).putExtra(Constants.MAIL_TYPE, Constants.MAIL_TYPE_TODAY_HOT).startActivity(true);
                break;
            case R.id.fiv_gyzq:
                new StartActivityUtil(context, TodayVogueActivity.class).putExtra(Constants.MAIL_TYPE, Constants.MAIL_TYPE_GYZQ).startActivity(true);
                break;
            case R.id.fiv_desq:
                new StartActivityUtil(context, TodayVogueActivity.class).putExtra(Constants.MAIL_TYPE, Constants.MAIL_TYPE_DESQ).startActivity(true);
                break;
            case R.id.layout_ms_right1:
                reSetLimitedSeckill();
                tvMsTime1.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                tvMsText1.setTextColor(ContextCompat.getColor(context, R.color.white));
                tvMsText1.setBackgroundResource(R.drawable.corner_primary_button_50);
                setLimitedSeckillList(2);
                break;
            case R.id.layout_ms_right2:
                reSetLimitedSeckill();
                tvMsTime2.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                tvMsText2.setTextColor(ContextCompat.getColor(context, R.color.white));
                tvMsText2.setBackgroundResource(R.drawable.corner_primary_button_50);
                setLimitedSeckillList(1);
                break;
            case R.id.layout_ms_right3:
                reSetLimitedSeckill();
                tvMsTime3.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                tvMsText3.setTextColor(ContextCompat.getColor(context, R.color.white));
                tvMsText3.setBackgroundResource(R.drawable.corner_primary_button_50);
                setLimitedSeckillList(0);
                break;
            case R.id.layout_hot_pintuan:
                //热门团购
//                new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
//                        1).startActivity(true);
                new StartActivityUtil(context, PintuanHotGroupActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                        1).startActivity(true);
                break;
            case R.id.layout_invite_friend:
                //邀请好友
                new StartActivityUtil(context, InviteFriendActivity.class).startActivity(true);
                break;
            case R.id.layout_new_teach:
                //新手教程
                new StartActivityUtil(context, MessageListActivity.class).putExtra(Constants.MESSAGE_TYPE, Constants.NEWCOURSE_MESSAGE)
                        .startActivity(true);
                break;
            case R.id.layout_go_tuanzhang:
            case R.id.iv_go_tuanzhang:
                //成为团长
                ((MainActivity) activity).jumpToVip();
                break;
            case R.id.layout_go_water:
            case R.id.iv_go_water:
                //水滴商城
                new StartActivityUtil(context, WaterHomeActivity.class).startActivity(true);
                break;
            case R.id.iv_to_top:
                //回到顶部
                nestedScrollView.fullScroll(NestedScrollView.FOCUS_UP);
                ivToTop.setVisibility(View.GONE);
                break;
            case R.id.lottie_redbaganim:
                //红包列表
                LoadingDialogUtils.show(activity);
                mPresenter.ptiLucydrawCheck();
                break;
            default:
                break;
        }
    }

    /**
     * 重置
     */
    public void reSetLimitedSeckill() {
        tvMsTime1.setTextColor(ContextCompat.getColor(context, R.color.color_111));
        tvMsTime2.setTextColor(ContextCompat.getColor(context, R.color.color_111));
        tvMsTime3.setTextColor(ContextCompat.getColor(context, R.color.color_111));

        tvMsText1.setTextColor(ContextCompat.getColor(context, R.color.color_999));
        tvMsText2.setTextColor(ContextCompat.getColor(context, R.color.color_999));
        tvMsText3.setTextColor(ContextCompat.getColor(context, R.color.color_999));

        tvMsText1.setBackground(null);
        tvMsText2.setBackground(null);
        tvMsText3.setBackground(null);
    }

    @Override
    public void showContent(String list) {

    }

    @Override
    public void showMoreContent(String list) {

    }

    @Override
    public void showLbImg(int type, List<LbImgBean> list) {
        //顶部轮播
        if (type == 1) {
            if (list != null && list.size() > 0) {
//            xbanner.setBannerData(R.layout.banner_item_with_corner, list);
                xbanner.setBannerData(list);
                xbanner.loadImage(new XBanner.XBannerAdapter() {
                    @Override
                    public void loadBanner(XBanner banner, Object model, View view, int position) {
                        ImageView iv = (ImageView) view;
                        iv.setScaleType(ImageView.ScaleType.FIT_XY);
                        Glide.with(context).load(list.get(position).getSrc()).dontAnimate().into(iv);
                    }
                });
                xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                    @Override
                    public void onItemClick(XBanner banner, Object model, View view, int position) {
                        try {
                            LbImgBean lbImgBean = list.get(position);
                            if (lbImgBean == null) {
                                return;
                            }
                            if (lbImgBean.getGoods_type() == 1) {
                                //一级分类
                                new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                        3).putExtra(Constants.PINTUAN_SHOP,
                                        lbImgBean.getGoods_id()).putExtra(Constants.PINTUAN_SHOP_TITLE,
                                        TextUtils.isEmpty(lbImgBean.getGoods_name()) ? "商品" : lbImgBean.getGoods_name())
                                        .startActivity(true);
                            } else if (lbImgBean.getGoods_type() == 2) {
                                //二级分类
                                new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                        2).putExtra(Constants.PINTUAN_SHOP,
                                        lbImgBean.getGoods_id())
                                        .putExtra(Constants.PINTUAN_SHOP_TITLE,
                                                TextUtils.isEmpty(lbImgBean.getGoods_name()) ? "商品" : lbImgBean.getGoods_name())
                                        .startActivity(true);
                            } else if (lbImgBean.getGoods_type() == 3) {
                                //商品id
                                new StartActivityUtil(activity, PintuanShopDetailsActivity.class).putExtra(Constants.PINTUAN_SHOP_ID,
                                        lbImgBean.getGoods_id())
                                        .startActivity(true);

                            } else if (lbImgBean.getGoods_type() == 4) {
                                //水滴商城
                                new StartActivityUtil(activity, WaterShopDetailsActivity.class).putExtra(Constants.WATER_SHOP_ID,
                                        lbImgBean.getGoods_id())
                                        .startActivity(true);

                            } else {
                                if (!TextUtils.isEmpty(lbImgBean.getJump())) {
                                    new StartActivityUtil(activity, MyWebviewActivity.class).putExtra("url", lbImgBean.getJump())
                                            .putExtra(Constants.WEBURL_TITLE, lbImgBean.getTitle()).startActivity(true);
                                }
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        } else if (type == 2) {
            //广告
            if (list != null && list.size() > 0) {
                ImageLoader.load(context, list.get(0).getSrc(), ivAd);
                fixAd.setVisibility(View.VISIBLE);
                fixAd.setOnClickListener(new OnDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {

                        try {
                            LbImgBean lbImgBean = list.get(0);
                            if (lbImgBean == null) {
                                return;
                            }
                            if (lbImgBean.getGoods_type() == 1) {
                                //一级分类
                                new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                        3).putExtra(Constants.PINTUAN_SHOP,
                                        lbImgBean.getGoods_id()).putExtra(Constants.PINTUAN_SHOP_TITLE,
                                        TextUtils.isEmpty(lbImgBean.getGoods_name()) ? "商品" : lbImgBean.getGoods_name())
                                        .startActivity(true);
                            } else if (lbImgBean.getGoods_type() == 2) {
                                //二级分类
                                new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                        2).putExtra(Constants.PINTUAN_SHOP,
                                        lbImgBean.getGoods_id())
                                        .putExtra(Constants.PINTUAN_SHOP_TITLE,
                                                TextUtils.isEmpty(lbImgBean.getGoods_name()) ? "商品" : lbImgBean.getGoods_name())
                                        .startActivity(true);
                            } else if (lbImgBean.getGoods_type() == 3) {
                                //商品id
                                new StartActivityUtil(activity, PintuanShopDetailsActivity.class).putExtra(Constants.PINTUAN_SHOP_ID,
                                        lbImgBean.getGoods_id())
                                        .startActivity(true);

                            } else if (lbImgBean.getGoods_type() == 4) {
                                //水滴商城
                                new StartActivityUtil(activity, WaterShopDetailsActivity.class).putExtra(Constants.WATER_SHOP_ID,
                                        lbImgBean.getGoods_id())
                                        .startActivity(true);

                            } else {
                                if (!TextUtils.isEmpty(lbImgBean.getJump())) {
                                    new StartActivityUtil(activity, MyWebviewActivity.class).putExtra("url", lbImgBean.getJump())
                                            .putExtra(Constants.WEBURL_TITLE, lbImgBean.getTitle()).startActivity(true);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
//                        new StartActivityUtil(activity, MyWebviewActivity.class).putExtra("url", list.get(0).getJump())
//                                .putExtra(Constants.WEBURL_TITLE, list.get(0).getTitle()).startActivity(true);
                    }
                });
            } else {
                fixAd.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void showViewFlipper(List<ArticleArticleResponBean> list) {
        if (list != null && list.size() > 0) {
            for (ArticleArticleResponBean bean : list) {
                TextView itemTextView = (TextView) View.inflate(context, R.layout.item_ewnews_viewflipper, null);
                itemTextView.setText(bean.getTitle());
                itemTextView.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        new StartActivityUtil(activity, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_TOUTIAO + bean.getId())
                                .putExtra(Constants.WEBURL_TITLE, bean.getTitle()).startActivity(true);
                    }
                });
                vfNews.addView(itemTextView);
            }
        }
    }

    @Override
    public void showLimitedSeckillList(HomeLimitedSeckillLocalTop bean) {
        homeLimitedSeckillLocalTop = bean;
        setLimitedSeckillList(0);
    }

    /**
     * 设置限时秒杀列表
     */
    private HomeLimitedSeckillAdapter homeLimitedSeckillAdapter;

    public void setLimitedSeckillList(int position) {
        List<HomeLimitedSeckillLocalOne> homeLimitedSeckillLocalOnes = null;
        if (homeLimitedSeckillLocalTop != null) {
            if (position == 0) {
                homeLimitedSeckillLocalOnes = homeLimitedSeckillLocalTop.getTime1();
            } else if (position == 1) {
                homeLimitedSeckillLocalOnes = homeLimitedSeckillLocalTop.getTime2();
            } else if (position == 2) {
                homeLimitedSeckillLocalOnes = homeLimitedSeckillLocalTop.getTime3();
            }
            if (homeLimitedSeckillLocalOnes == null) {
                homeLimitedSeckillLocalOnes = new ArrayList<>();
            }
            if (homeLimitedSeckillLocalOnes.size() > 3) {
                homeLimitedSeckillLocalOnes = homeLimitedSeckillLocalOnes.subList(0, 3);
            }
            if (homeLimitedSeckillAdapter == null) {
                recyclerViewXsms.setNestedScrollingEnabled(false);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
                gridLayoutManager.setSmoothScrollbarEnabled(true);
                recyclerViewXsms.setLayoutManager(gridLayoutManager);
                homeLimitedSeckillAdapter = new HomeLimitedSeckillAdapter(R.layout.item_home_limited_seckill);
                homeLimitedSeckillAdapter.setNewData(homeLimitedSeckillLocalOnes);
                homeLimitedSeckillAdapter.setHasStableIds(true);
                recyclerViewXsms.setAdapter(homeLimitedSeckillAdapter);
            } else {
                homeLimitedSeckillAdapter.setNewData(homeLimitedSeckillLocalOnes);
            }
        }
    }

    @Override
    public void showTbyoulike(List<MailShopResponBean> list) {
        if (list != null) {
            recyclerViewTuijian.setNestedScrollingEnabled(false);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
            linearLayoutManager.setSmoothScrollbarEnabled(true);
            recyclerViewTuijian.setLayoutManager(linearLayoutManager);
            MailShopAdapter mailShopAdapter = new MailShopAdapter(R.layout.item_mail_shop);
            mailShopAdapter.setNewData(list);
            mailShopAdapter.setHasStableIds(true);
            recyclerViewTuijian.setAdapter(mailShopAdapter);
        }
    }

    @Override
    public void showPintuanList(List<PintuanShopListResponBean> list) {
        //获取拼团成功
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            pintuanShopListAdapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            pintuanShopListAdapter.addData(list);
        }

        if (pintuanShopListAdapter.getData().size() > 0) {
            tvWntj.setVisibility(View.VISIBLE);
        } else {
            tvWntj.setVisibility(View.GONE);
        }
    }

    @Override
    public void showPintuanListFail() {
        //获取拼团失败
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
        if (pintuanShopListAdapter.getData().size() == 0) {
            tvWntj.setVisibility(View.GONE);
        }
    }

    @Override
    public void showPticatesAll(List<PintuanClassifyResponBean> list) {
        //全部分类
        List<MyPageGridBean> classifyList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (PintuanClassifyResponBean bean : list) {
                MyPageGridBean myPageGridBean = new MyPageGridBean();
                myPageGridBean.setId(bean.getId());
                myPageGridBean.setTitle(bean.getName());
                myPageGridBean.setImg(bean.getImage());
                classifyList.add(myPageGridBean);
            }
        }
        pageView.setData(classifyList);
        pageView.setOnItemClickListener(new PageGridView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                try {
                    if (classifyList.get(position).getId() == 118) {
//                        if ((TbsVideo.canUseTbsPlayer(activity))) {
                        //可以播放视频
                        new StartActivityUtil(context, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_VIDEO)
                                .putExtra(Constants.WEBURL_TITLE, classifyList.get(position).getTitle()).startActivity(true);
//                        } else {
//                            StyleableToast.makeText(context, "视频播放器没有准备好", Toast.LENGTH_SHORT, R.style.mytoast).show();
//                        }
                    } else if (classifyList.get(position).getId() == 269) {
                        //小说
                        new StartActivityUtil(context, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_BOOKS)
                                .putExtra(Constants.WEBURL_TITLE, classifyList.get(position).getTitle()).startActivity(true);
                    } else {
                        new StartActivityUtil(context, PintuanHotActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                3).putExtra(Constants.PINTUAN_SHOP,
                                classifyList.get(position).getId()).putExtra(Constants.PINTUAN_SHOP_TITLE,
                                classifyList.get(position).getTitle())
                                .startActivity(true);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 红包弹框
     */
    private void showRedbagPopup() {
        RedBagPopup redBagPopup = new RedBagPopup(activity,
                new PopupItemClickCallback() {

                    @Override
                    public void onSureCallback(String text) {
                        //检测红包状态
                        LoadingDialogUtils.show(activity);
                        mPresenter.ptiLuckydraw();
                    }

                    @Override
                    public void onTextChangedCallback(String text) {

                    }
                });
        redBagPopup.setPopupGravity(Gravity.CENTER);
        redBagPopup.showPopupWindow();

    }

    @Override
    public void showPtiLucydrawCheck(BaseResponse baseResponse) {
        //检测红包 返回数据
        LoadingDialogUtils.dismissDialog_ios();
        if (baseResponse != null && !TextUtils.isEmpty(baseResponse.getNum())) {
            if (baseResponse.getNum().equals("1")) {
                //一次领取资格  打开红包
                showRedbagPopup();
            } else if (baseResponse.getNum().equals("2")) {
                //已领取直接跳领取列表
                new StartActivityUtil(context, RedEnvelopesActivity.class).startActivity(true);
            }
        }

    }

    private CustomBasePopup customBasePopup;

    @Override
    public void showPtiLucydrawCheckError(int code, String message) {
        if (code == 1) {
            //弹框
            if (TextUtils.isEmpty(message)) {
                return;
            }
//            CustomTipPopup mCustomTipPopup = new CustomTipPopup(activity, "提示",
//                    message, null);
//            mCustomTipPopup.setPopupGravity(Gravity.CENTER);
//            mCustomTipPopup.setWidth(mCustomTipPopup.getScreenWidth() * 2 / 3);
//            mCustomTipPopup.showPopupWindow();

            if (customBasePopup != null) {
                customBasePopup.showPopupWindow();
                return;
            }
            customBasePopup = new CustomBasePopup(activity, "红包资格提示", "\u0020去拼团\u0020", "\u3000取消\u3000",
                    new CustomBasePopup.PopupWindowCallback() {
                        @Override
                        public void sureClickCallback() {
                            //去拼团
                            new StartActivityUtil(context, PintuanHotGroupActivity.class).putExtra(Constants.PINTUAN_SHOP_TYPE,
                                    1).startActivity(true);
                        }

                        @Override
                        public void cancelClickCallback() {

                        }
                    });
            customBasePopup.setContentText(message);
            customBasePopup.setPopupGravity(Gravity.CENTER);
            customBasePopup.showPopupWindow();
        }
    }

    @Override
    public void showPtiLuckydraw(BaseResponse baseResponse) {
        //领红包完成
        LoadingDialogUtils.dismissDialog_ios();
        new StartActivityUtil(context, RedEnvelopesActivity.class).startActivity(true);
    }

    @Override
    public void showPtiLuckyDrawoff(BaseResponse baseResponse) {
        //检测红包是否显示
        if (baseResponse != null && baseResponse.getIs_show() == 1) {
            //显示红包
            lottieRedbaganim.setVisibility(View.VISIBLE);
        } else {
            //隐藏红包
            lottieRedbaganim.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (vfNews != null) {
            vfNews.stopFlipping();
            vfNews = null;
        }
    }
}
